﻿using System.Linq;
using iss.sg4210.se18.team6s.jass.DAL.DataAccess.Interfaces;

namespace iss.sg4210.se18.team6s.jass.DAL.DataAccess.MSSQL
{
    public class EventsDAO : ObjectDAO, IEvents
    {
        public EventsDAO() : base() { }

        public System.Linq.IQueryable<Event> FindAllEvents()
        {
            return jassEntities.Events;
        }

        public Event GetEvent(int eventId)
        {
            return jassEntities.Events.SingleOrDefault(c => c.Id == eventId);
        }

        public void AddEvent(Event eventObject)
        {
            jassEntities.AddToEvents(eventObject);
            Save();
        }

        public void UpdateEvent(Event eventObject)
        {
            jassEntities.Events.ApplyCurrentValues(eventObject);
            Save();
        }

        public void DeleteEvent(int eventId)
        {
            Event eventObject = GetEvent(eventId);
            jassEntities.Events.DeleteObject(eventObject);
            Save();
        }


        public IQueryable<Event> FindAllEventsByUser(System.Guid userId)
        {
            return jassEntities.Events.Where(m => m.UserId == userId);
        }
    }

    public class ArticleDAO : ObjectDAO, IArticles
    {
        public ArticleDAO() : base() { }

        public System.Linq.IQueryable<Article> FindAllArticles()
        {
            return jassEntities.Articles;
        }

        public Article GetArticle(int articleId)
        {
            return jassEntities.Articles.SingleOrDefault(c => c.Id == articleId);
        }

        public void AddArticle(Article article)
        {
            jassEntities.AddToArticles(article);
            Save();
        }

        public void UpdateArticle(Article article)
        {
            jassEntities.Articles.ApplyCurrentValues(article);
            Save();
        }

        public void DeleteArticle(int articleId)
        {
            Article article = GetArticle(articleId); ;
            jassEntities.Articles.DeleteObject(article);
            Save();
        }


        public IQueryable<Article> FindAllArticlesByUser(System.Guid userId)
        {
            return jassEntities.Articles.Where(m => m.UserId == userId);
        }
    }

}
